package cqupt.spark_log.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {
	public static Date getTime(String content) {
		Pattern pattern = Pattern.compile("\\d+/\\d+/\\d+ (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d");
		Matcher matcher = pattern.matcher(content);
		if (matcher.find()) {
			try {
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
				String dateString = matcher.group(0);
				Date result = simpleDateFormat.parse(dateString);
				return result;
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		} else {
			return null;
		}
	}

	public static Date getMonitorTime(String content) {
		Pattern pattern = Pattern.compile("\\d+-\\d+-\\d+ (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d");
		Matcher matcher = pattern.matcher(content);
		if (matcher.find()) {
			try {
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String dateString = matcher.group(0);
				Date result = simpleDateFormat.parse(dateString);
				return result;
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		} else {
			return null;
		}
	}
	
	public static void printMap(Map<?, ?> map) {
		for (Object key : map.keySet()) {
			System.out.println(map.get(key).toString());
		}
	}

	public static String getExperimentPath(String bASE_PATH) {
		String[] paths = null;
		if (bASE_PATH.contains("\\")) {
			paths = bASE_PATH.split("\\");
		} else {
			paths = bASE_PATH.split("/");
		}
		int index = paths[paths.length-1].length()+paths[paths.length-2].length()+1;
		String path = bASE_PATH.substring(0,bASE_PATH.length()-index);
		return path;
	}
}
